<template>
  <el-container class="home-container">
    <!-- 头部部分 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" />
        <span>商城后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出登录</el-button>
    </el-header>
    <!-- 内容部分 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <!-- 侧边栏折叠按钮 -->
        <div class="toggle-btn" @click="toggleAside">|||</div>
        <!-- 侧边栏菜单区域 -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="nowActive"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menuList"
            :key="item.id"
          >
            <!-- 一级菜单模板 -->
            <template slot="title">
              <!-- 菜单图标 -->
              <i :class="iconObj[item.id]"></i>
              <!-- 菜单标题 -->
              <span>{{ item.authName }}</span>
            </template>
            <!-- 菜单项 -->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="active('/' + subItem.path)"
            >
              <template slot="title">
                <!-- 菜单项图标 -->
                <i class="el-icon-menu"></i>
                <!-- 菜单项标题 -->
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体部分 -->
      <el-main>
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data: function () {
    return {
      //侧边栏列表
      menuList: [],
      iconObj: {
        125: "iconfont icon-user",
        103: "iconfont icon-tijikongjian",
        101: "iconfont icon-shangpin",
        102: "iconfont icon-danju",
        145: "iconfont icon-baobiao",
      },
      //侧边栏是否折叠
      isCollapse: false,
      //当前激活菜单的index
      nowActive: ''
    };
  },
  //组件创建钩子函数
  created: async function () {
    //组件创建时发送请求获取测导航栏列表
    const { data: res } = await this.$http.get("menus");
    if (res.meta.status != 200) return this.$message.error(res.meta.msg);
    this.menuList = res.data;
    this.nowActive = sessionStorage.getItem('nowActive')
  },
  methods: {
    logout: function () {
      //清除session
      window.sessionStorage.clear();
      //跳转至登录页
      this.$router.push("/login");
    },
    //切换侧边栏状态
    toggleAside: function () {
      this.isCollapse = !this.isCollapse;
    },
    active: function(index){
        sessionStorage.setItem('nowActive', index)
        this.nowActive = index
    }
  },
};
</script>

<style lang="less" scoped>
.el-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 0;
  background-color: #373d41;
  > div {
    display: flex;
    align-items: center;
    span {
      padding-left: 15px;
      color: #fff;
      font-size: 20px;
    }
  }
}
.el-aside {
  transition: all 0.2s;
  background-color: #333744;
  .el-menu {
    border-right: 0;
  }
}
.el-main {
  background-color: #eaedf1;
}
.home-container {
  height: 100%;
  background-color: red;
}
.iconfont {
  padding-right: 10px;
}

.toggle-btn {
  color: #fff;
  line-height: 34px;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
  background-color: #111424;
}
</style>